package Util::CreditTuanZhang::CreditTuanZhang;
use Moose;
use namespace::autoclean;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub create {
	my ($self,$type_id,$credit,$tuanzhang_id,$tuan_id,$user_id) = @_;
	my $credit_tuan = $self->schema->resultset('CreditTuanZhang')->create({
                        type_id => $type_id,
                        credit => $credit,
                        tuanzhang_id => $tuanzhang_id,
                        tuan_id => $tuan_id,
                        user_id => $user_id,
                        created_by => $user_id,
                        updated_by => $user_id
                        });
	return $credit_tuan;
}

sub update {
	my ($self,$type_id,$credit,$tuan_id,$user_id) = @_;
	my $credit_tuan = $self->schema->resultset('CreditTuanZhang')->search({'user_id' => $user_id, 'tuan_id' => $tuan_id, 'type_id' => $type_id},{})->first();
	$credit_tuan->update({	
                        credit => $credit,
                        updated_by => $user_id
                        });
	return $credit_tuan;
}

sub find_credit_by_user_tuan_type {
        my ($self, $user_id, $tuan_id, $type_id) = @_;
        return $self->schema->resultset('CreditTuanZhang')->search({'user_id' => $user_id, 'tuan_id' => $tuan_id, 'type_id' => $type_id},{})->first();
}

sub find_credit_by_tuan_type {
        my ($self, $tuan_id, $type_id) = @_;
        return $self->schema->resultset('CreditTuanZhang')->search({'tuan_id' => $tuan_id, 'type_id' => $type_id},{});
}

sub find_credit_by_tuan {
        my ($self, $tuan_id) = @_;
        return $self->schema->resultset('CreditTuanZhang')->search({'tuan_id' => $tuan_id},{});
}

__PACKAGE__->meta->make_immutable;

1;
